//
//  UserDetailViewController.swift
//  todaySwift
//
//  Created by ChokWah on 2018/3/24.
//  Copyright © 2018年 ChokWah. All rights reserved.
//

import UIKit

class UserDetailViewController: UIViewController {

    @IBOutlet weak var bottomView: UIView!
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var bottomViewHeight: NSLayoutConstraint!
    
    @IBOutlet weak var bottomViewBottom: NSLayoutConstraint!
    
    
    // 用户ID
    var user_id: Int = 0
    var userDetail: UserDetail?
    // 改变状态栏的字体颜色
    var changeStatusBarStyle: UIStatusBarStyle = .lightContent {
        didSet { // 当设置的时候，更新
            setNeedsStatusBarAppearanceUpdate()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        scrollView.addSubview(headerView)
        scrollView.contentSize = CGSize(width: screenWidth, height: 1000)
        scrollView.delegate = self
        view.addSubview(navigationBar)
        navigationBar.goBackButtonClicked = {
            self.navigationController?.popViewController(animated: true)
        }
        bottomViewBottom.constant = isIPhoneX ? 34 : 0
        view.layoutIfNeeded()
        
        NetWorkTool.loadUserDetail(user_id: user_id) { (userDetail) in
            NetWorkTool.loadUserDetailDongtaiList(user_id: self.user_id, completionHandler: { (dongtais) in
                self.userDetail = userDetail
                self.headerView.userDetail = userDetail
                self.headerView.dongtais = dongtais
                if userDetail.bottom_tab.count == 0 {
                    self.bottomViewBottom.constant = 0
                    self.view.layoutIfNeeded()
                }else {
                    // 创建view 赋值
                    self.bottomView.addSubview(self.myBottomView)
                    self.myBottomView.bottomTabs = userDetail.bottom_tab
                }
            })
        }
    }
    
    // MARK: 懒加载
    fileprivate lazy var headerView: UserDetailHeaderView = {
        let headerView = UserDetailHeaderView.loadViewFromNib()
        return headerView
    }()
    
    // 之前已经有个bottomView
    fileprivate lazy var myBottomView: UserDetailBottomView = {
        let myBottomView = UserDetailBottomView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 44))
        myBottomView.delegate = self
        return myBottomView
    }()
    
    // 导航条
    fileprivate lazy var navigationBar: NavigationBarView = {
        let navigationBarView = NavigationBarView.loadViewFromNib()
        return navigationBarView
    }()
    
    override func viewWillAppear(_ animated: Bool) {
         super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(true, animated: animated)
    }
}

// MARK: 滑动代理方法
extension UserDetailViewController: UIScrollViewDelegate {
    private func scrollletwDidScroll(_ scrollView: UIScrollView) {
        let offsetY = scrollView.contentOffset.y
        // 下拉的时候，图片黏住顶部，拉伸
        if offsetY < -44 { // 说明是下拉，静止时距离顶部是44，下拉比-44还少
                let totalOffset = kUserDetailHeaderBGImageViewHeight  + abs(offsetY)
            let f = totalOffset / kUserDetailHeaderBGImageViewHeight
            headerView.backgroundImageView.frame = CGRect(x: -screenWidth * (f - 1) * 0.5, y: offsetY, width: screenWidth * f, height: totalOffset)
            navigationBar.backgroundColor = UIColor(white: 1.0, alpha: 0.0)
        }else {
            var alpha:CGFloat = (offsetY + 44) / 58
            alpha = min(alpha, 1.0)
            navigationBar.backgroundColor = UIColor(white: 1.0, alpha: alpha)
            if alpha == 1.0 { // 需要
                changeStatusBarStyle = .default
                navigationBar.returnButton.theme_setImage("images.personal_home_back_black_24x24_", forState: .normal)
                navigationBar.moreButton.theme_setImage("images.new_morewhite_titlebar_22x22_", forState: .normal)
            } else {
                changeStatusBarStyle = .lightContent
                navigationBar.returnButton.theme_setImage("images.personal_home_back_white_24x24_", forState: .normal)
                navigationBar.moreButton.theme_setImage("images.new_morewhite_titlebar_22x22_", forState: .normal)
            }
            
            // 14(当offset为0，顶部距离为14) + 15(关注按钮距离顶部) + 14(关注按钮的一半)
            var alpha_1: CGFloat = offsetY / 57 //(上面的43加14，关注按钮的另一半已经覆盖过)
            if offsetY >= 43 {
                alpha_1 = min(alpha_1, 1.0)
                navigationBar.nameLabel.isHidden = false
                navigationBar.concernButton.isHidden = false
                navigationBar.nameLabel.textColor = UIColor(r: 0, g: 0, b: 0, alpha: alpha_1)
                navigationBar.concernButton.alpha = alpha_1
            } else {
                alpha_1 = min(0.0, alpha_1)
                navigationBar.nameLabel.textColor = UIColor(r: 0, g: 0, b: 0, alpha: alpha_1)
                navigationBar.concernButton.alpha = alpha_1
            }
        }
    }
}

// MARK: 底部按钮点击
extension UserDetailViewController: UserDetailBottomViewDelegate {
    func bottomView(clicked button: UIButton, bottomTab: BottomTab) {
        let bottomPushVC = UserDetailBottomPushController()
        bottomPushVC.navigationItem.title = "网页浏览"
        if bottomTab.children.count == 0 { // 直接跳转到下一个控制器
            bottomPushVC.url = bottomTab.value
            navigationController?.pushViewController(bottomPushVC, animated: true)
        }else { // 弹出指示图
            // 获取button位置，转化为弹出指示图的位置
            let sb = UIStoryboard(name: "\(UserDetailBottomPushController.self)", bundle: nil)
            let popVC = sb.instantiateViewController(withIdentifier: "\(UserDetailBottomPopController.self)") as! UserDetailBottomPopController
            popVC.children = bottomTab.children
            popVC.didSelectedChild = {
                bottomPushVC.url = $0.value
                self.navigationController?.pushViewController(bottomPushVC, animated: true)
            }
            // 定义转场动画
            popVC.modalPresentationStyle = .custom
            // 坐标转换
            let rect = bottomView.convert(button.frame, to: view)
            let popWidth = (screenWidth - CGFloat((userDetail?.bottom_tab.count)! + 1) * 20) / CGFloat((userDetail?.bottom_tab.count)!)
            let popX = CGFloat(button.tag) * (popWidth + 20) + 20
            let popHeight = CGFloat(bottomTab.children.count) * 40 + 25
            let popAnimator = PopoverAnimator()
            popAnimator.presentFrame = CGRect(x: popX, y: rect.origin.y - popHeight, width: popWidth, height: popHeight)
            popVC.transitioningDelegate = popAnimator
            present(popVC, animated: true, completion: nil)
        }
    }
}
